package com.biubiushop.Socket.TcpAndUdp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author chenjia
 * @date 2019/7/17
 **/
public class UDPClient {

    public static void main(String[] args) throws Exception {
        InetAddress address = InetAddress.getByName("localhost");
        int port = 8800;
        String str = "我是客户端";
        byte[] bytes = str.getBytes();

        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, address, port);
        DatagramSocket datagramSocket = new DatagramSocket();
        datagramSocket.send(datagramPacket);

        byte[] bytes1 = new byte[1024];
        DatagramPacket datagramPacket1 = new DatagramPacket(bytes1, bytes1.length);
        datagramSocket.receive(datagramPacket1);

        String info = new String(bytes1);
        System.out.println("客户端收到服务端的消息为:" + info);

        datagramSocket.close();


    }
}
